package com.czkj.hwz.decorate.bean;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czkj.hwz.decorate.R;

import java.util.List;

/**
 * Created by Administrator on 2017/10/7.
 */

public abstract class MyTab {
    private View mView;
    private LinearLayout ll_main;
    private List data;
    private Context mContext;
    private Object itemData;
    boolean isH = true;
    private TextView tv_qb;
    public MyTab(Context context,int flag) {
        mContext = context;

        if (flag == LinearLayout.HORIZONTAL) {
            mView = LayoutInflater.from(context).inflate(R.layout.mytab_h, null);
        }else if (flag == LinearLayout.VERTICAL){
            mView = LayoutInflater.from(context).inflate(R.layout.mytab, null);
            isH = false;
        }else {
            mView = LayoutInflater.from(context).inflate(R.layout.mytab_h, null);
        }
        ll_main = (LinearLayout) mView.findViewById(R.id.ll_main);

            ll_main.setBackgroundColor(mContext.getResources().getColor(R.color.white));

    }

    public void setData(List data) {
        this.data = data;
    }

    public void setText_Qb(TextView qb){
        this.tv_qb = qb;

    }


    public View getView() {
        if (data != null) {
            for (int i = 0; i < data.size(); i++) {
                itemData = data.get(i);
               final TextView item = new TextView(mContext);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                item.setGravity(Gravity.CENTER);
                item.setPadding(20,40,20,40);
//                item.setPadding(30,60,30,60);
                if (!isH) {
                    params.setMargins(0,1,0,0);
//                    params.setMargins(0,4,0,0);
//                    params.setMargins(0,4,0,4);
                    item.setBackgroundColor(mContext.getResources().getColor(R.color.grey));
                }
                item.setLayoutParams(params);
                item.setTextSize(15);
                item.setTextColor(mContext.getResources().getColor(R.color.textColor));
                item.setTag(i);
                item.setText(itemData.toString());
                item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        changeColor((int)item.getTag());
                        onItemClickListener((int)item.getTag());

                    }
                });
                ll_main.addView(item);
            }
        }

        return mView;
    }

    public void changeColor(int position) {
        if (position != -1 ) {
            if (null!=tv_qb) {
                tv_qb.setTextColor(mContext.getResources().getColor(R.color.textColor));
            }
        }

        for (int i = 0; i <data.size() ; i++) {

            TextView item = (TextView) ll_main.getChildAt(i);
            if (i != position) {
                item.setTextColor(mContext.getResources().getColor(R.color.textColor));
                if (!isH) {
                    item.setBackgroundColor(mContext.getResources().getColor(R.color.grey));
                }

            }else {
                item.setTextColor(mContext.getResources().getColor(R.color.yellow));
                if (!isH) {
                    item.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                }
            }
        }
    }
    public abstract void onItemClickListener(int poistion) ;
    public void setItemColor(int position) {
        TextView item = (TextView) ll_main.getChildAt(position);
        item.setTextColor(mContext.getResources().getColor(R.color.yellow));
    }
}
